<template>
  <el-form  v-loading="loading" :model="lost" ref="lostRef" label-width="120px" :rules ="lostRule">
    <el-form-item label="拾到人" prop="gleanMan">
      <el-input v-model="lost.gleanMan" />
    </el-form-item>
    <el-form-item label="记录人" prop="enterMan">
      <el-input v-model="lost.enterMan" />
    </el-form-item>
    <el-form-item label="物品名称" prop="lostName">
      <el-input v-model="lost.lostName" />
    </el-form-item>
    <el-form-item label="文件上传" requierd>
      <el-upload
        ref="upload"
        class="upload-demo"
        :action="uploadUrl"
        :limit="1"
        :on-exceed="handleExceed"
        :before-upload="checkType"
        :on-success="uploadSuccess"
        :on-error="uploadError"
        :on-remove="handleRemove"
        :auto-upload="true"
        :headers="headers"
        >
        <el-icon class="el-icon--upload"><upload-filled /></el-icon>
        <div class="el-upload__text">
          点击上传
          <!--Drop file here or <em>click to upload</em>-->
        </div>
      </el-upload>
      <el-input type="hidden" v-model="lost.fileRandomId"></el-input>
    </el-form-item>
    <el-form-item label="丢失地址" prop="originDepart">
      <el-input v-model="lost.originDepart" />
    </el-form-item>
    <el-form-item label="描述" prop="fileDesc">
      <el-input v-model="lost.fileDesc" type="textarea" />
    </el-form-item>
    <el-form-item>
      <!-- 先不做保存 -->
      <!-- <el-button type="primary" @click="onSubmit">{{$t('msg.common.save')}}</el-button> -->
      <el-button type="primary" @click="onSubmit">发布</el-button>
      <el-button @click="onReset">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from 'vue'
import { UPLOAD_URL } from '@/const'
import { useI18n } from 'vue-i18n'
import { add } from '@/api/lost'
import { ElMessage } from 'element-plus'
import { deleteByUuid } from '@/api/localFile'
import { UploadFilled } from '@element-plus/icons-vue'
import store from '@/store'

const loading = ref(false)

const lostRef = ref(null)

const i18n = useI18n()

const lost = ref({
  gleanMan: '',
  enterMan: '',
  fileRandomId: '',
  lostName: '',
  originDepart: '',
  fileDesc: ''
})

const lostRule = ref({
  gleanMan: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.gleanMan')
    }
  ],
  enterMan: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.enterMan')
    }
  ],
  fileRandomId: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.fileRandomId')
    }
  ],
  lostName: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.lostName')
    }
  ],
  originDepart: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.originDepart')
    }
  ],
  fileDesc: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.lostRules.fileDesc')
    }
  ]
})

// 设置token
const headers = ref({ x_token: `${store.getters.token}` })

const uploadUrl = UPLOAD_URL

const handleExceed = (files, fileList) => {
  ElMessage.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
}

// 附件移除
const handleRemove = (file, uploadFile) => {
  if (lost.value.fileRandomId) {
    deleteByUuid(lost.value.fileRandomId)
  }
}

const checkType = file => {
  console.log(file)
  if (file.type.indexOf('image') !== 0) {
    ElMessage.warning(`当前选择的文件类型为 ${file.type}，只允许上传图片`)
  }
}
const uploadSuccess = res => {
  lost.value.fileRandomId = res.uuidName
  loading.value = false
}
const uploadError = res => {
  console.log(res)
}

// const addEntity = add(lost.value)
const onSubmit = () => {
  lost.value.statusDesc = 'PUBLIC'
  lostRef.value.validate(vaild => {
    if (!vaild) return
    loading.value = true
    add(lost.value).then(data => {
      ElMessage.success('成功')
      loading.value = false
      lost.value = {}
    }).catch(err => {
      console.log(err)
      ElMessage.error(err.message)
      loading.value = false
    })
  })
}

const onReset = () => {
  lost.value = {}
}
</script>

<style>

</style>
